﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;

namespace IpToBlack.PUB
{
    public class gFwM
    {        
        public static bool AddIPBlockRule1(string ipAddress)
        {
            // PowerShell脚本的路径
            string scriptPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SaasIPfw.ps1");
            string ruleName = "BLOCK_LIST_" + DateTime.Now.ToString("yyyy-MM-dd") + "_" + ipAddress;

            PUB.gFunc.WriteLog($"添加规则: " + ruleName);
            PUB.gFunc.WriteLog($"IP: " + ipAddress);

            // 构造PowerShell命令行
            var startInfo = new ProcessStartInfo
            {
                FileName = "powershell.exe",
                Arguments = $"-ExecutionPolicy Bypass -File \"{scriptPath}\" -RuleName \"{ruleName}\" -IPAddress \"{ipAddress}\"",
                UseShellExecute = false,
                CreateNoWindow = true,
                RedirectStandardOutput = true,
                RedirectStandardError = true
            };

            // 执行PowerShell脚本
            using (var process = new Process { StartInfo = startInfo })
            {
                process.Start();

                // 读取输出和错误（如果需要）
                string output = process.StandardOutput.ReadToEnd();
                string error = process.StandardError.ReadToEnd();

                process.WaitForExit();

                // 处理输出和错误（如果需要）
                Console.WriteLine(output);
                if (!string.IsNullOrEmpty(error))
                {
                    Console.WriteLine("Error: " + error);
                    return false;

                }
                else
                {
                    return true;
                }
            }
        }
        public static bool AddIPBlockRule2(List<string> ipAddress)
        {
            // PowerShell脚本的路径
            string scriptPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SaasIPfw2.ps1");
            string ruleName = "BLOCK_LIST_" + DateTime.Now.ToString("yyyy-MM-dd");
            string ips = string.Join(",", ipAddress);

            PUB.gFunc.WriteLog($"添加规则: " + ruleName);
            PUB.gFunc.WriteLog($"IP: " + ips);

            // 构造PowerShell命令行
            var startInfo = new ProcessStartInfo
            {
                FileName = "powershell.exe",
                Arguments = $"-ExecutionPolicy Bypass -File \"{scriptPath}\" -RuleName \"{ruleName}\" -IPAddresses  \"{ips}\"",
                UseShellExecute = false,
                CreateNoWindow = true,
                RedirectStandardOutput = true,
                RedirectStandardError = true
            };

            // 执行PowerShell脚本
            using (var process = new Process { StartInfo = startInfo })
            {
                process.Start();

                // 读取输出和错误（如果需要）
                string output = process.StandardOutput.ReadToEnd();
                string error = process.StandardError.ReadToEnd();

                process.WaitForExit();

                // 处理输出和错误（如果需要）
                Console.WriteLine(output);
                if (!string.IsNullOrEmpty(error))
                {
                    Console.WriteLine("Error: " + error);
                    return false;

                }
                else
                {
                    return true;
                }
            }
        }
        public static bool AddIPBlockRule3(List<string> ipAddress)
        {

            string ruleName = "BLOCK_LIST_" + DateTime.Now.ToString("yyyy-MM-dd");
            string ips = string.Join(",", ipAddress);

            PUB.gFunc.WriteLog($"添加规则: " + ruleName);
            PUB.gFunc.WriteLog($"IP: " + ips);

            // 构建netsh命令
            string command = $"netsh advfirewall firewall add rule name=\"{ruleName}\" dir=in action=block remoteip=\"{ips}\"";

            // 创建Process对象
            Process process = new Process();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.Arguments = $"/c {command}";
            process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.UseShellExecute = false;

            try
            {
                process.Start();
                process.WaitForExit();
                Console.WriteLine("规则添加成功。");
            }
            catch (Exception ex)
            {
                Console.WriteLine("添加规则时出错：" + ex.Message); 
                PUB.gFunc.WriteLog($"添加规则时出错: " + ex.Message + ex.StackTrace);
                return false;

            }
            return true;
        }
    }
}
